<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div style="text-align: center;font-size: 20px">上级审批</div>
    <div class="layui-form-item" id="formTable" style="padding-top: 3px">

    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">审批意见</label>
        <div class="layui-input-block">
            <textarea id="comment" name="comment" class="layui-textarea"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">选择</label>
        <div class="layui-input-block">
            <input type="radio" name="state" lay-filter="selectRadio" value="2" title="批准" checked>
            <input type="radio" name="state" lay-filter="selectRadio" value="3" title="驳回">
        </div>
    </div>

    <div id="sp">


    </div>
    <div id="spp">


    </div>

    <input type="hidden" name="ids" id="ids" />
    <input type="hidden" name="userId" id="userId" />
    <input type="hidden" name="taskId" id="taskId" />

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="showAdd">确定</button>
        </div>
    </div>
</div>
<div style="text-align: center;font-size: 20px;padding-top: 5px">批注列表</div>
<div>
    <table class="layui-table">
        <colgroup>
            <col width="150">
            <col width="150">
            <col width="200">
            <col width="150">
            <col width="200">
        </colgroup>
        <thead>
        <tr>
            <th>名称</th>
            <th>开始时间</th>
            <th>结束时间</th>
            <th>审批人</th>
            <th>审批内容</th>
        </tr>
        </thead>
        <tbody id="tbody">

        </tbody>

    </table>
</div>
<script src="../../lib/layui-v2.5.5/layui.all.js" charset="utf-8"></script>
<script src="../../js/common.js"></script>
<script>
    var processInstanceId;
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        processInstanceId = getUrlParam("processInstanceId");

        var taskId = getUrlParam("taskId");

        var processInstanceKey = getUrlParam("processInstanceKey");

        $("#taskId").val(taskId);

        $.ajax({
            type: "post",
            dataType: "json",
            url: webUrl + '/formTable/write_html_file', //url
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({
                'processInstanceKey': processInstanceKey
            }),
            success: function(result) {
                if (result.data.success == true) {
                    var t = result.data.formTable;
                    $("#formTable").html(t.formContent);
                }
            },
            error: function() {
                layer.msg("操作失败");
            }

        });
        var userList=[];

        /**
         * 获取用户
         */
        $.ajax({
            async : false,
            type:"post",
            dataType:"json",
            url: webUrl+'/user/getUserList',//url
            contentType: "application/json; charset=utf-8",
            success: function (result) {
                if (result.data.success==true) {
                    for(var i=0;result.data.rows.length>i;i++){
                        var users=[];
                        var id=result.data.rows[i].id;
                        var name=result.data.rows[i].firstName+result.data.rows[i].lastName;
                        users.push(id,name)
                        userList.push(users);
                    }
                } else {
                    layer.msg("操作失败");
                }
            },
            error: function () {
                layer.msg("操作失败");
            }

        })

        var pvmList;
        $.ajax({
            async: false,
            type: "post",
            dataType: "json",
            url: webUrl + '/task/findActiviti', //url
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({'taskId':taskId}),
            success: function(result) {
                //清空旧的html
                $('#sp').html('');
                var pvm=result.data;
                pvmList=pvm.listName;
                switch(pvm.pvmType){
                    case 'userTask':
                        //普通节点
                        selectPv(pvm.listName);
                        break;
                    case 'exclusiveGateway':
                        //排他网关
                        //设置流程变量
                        $('#spp').html('<input type="hidden" name="acKey" value="'+pvm.acKey+'">'+
                            '<input type="hidden" name="elValue" value="'+pvm.elValue+'">');

                        selectPv(pvm.listName);
                        break;
                    case 'parallelGateway':
                        //并行网关
                        selectPv(pvm.listName);
                        break;
                    case 'endEvent':
                        //结束节点
                        $('#spp').html('<input type="hidden" name="endEvent" value="endEvent">');
                        break;
                }

            },
            error: function() {
                layer.msg("操作失败");
            }

        })

        //批注列表
        $.ajax({
            type: "post",
            dataType: "json",
            url: webUrl + '/task/commentList', //url
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({
                'processInstanceId': processInstanceId,
            }),
            success: function(result) {
                if (result.data.rows.length > 0) {
                    var t = result.data.rows;
                    var tbody = "";
                    for (var i in t) {
                        var tt = t[i];
                        var endTime=tt.endTime==null?'':showTime(tt.endTime);

                        tbody += '<tr><td>' + tt.name + '</td><td>' + showTime(tt.createTime) + '</td>'+
                            '<td>' + endTime + '</td><td>' + tt.memberName +'</td><td>' + tt.comment +'</td></tr>';
                    }
                    $("#tbody").html(tbody);

                }
            },
            error: function() {
                layer.msg("操作失败");
            }

        })

        //监听提交
        form.on('submit(showAdd)', function(data) {
            var ids='';

            for(var i in pvmList){
                var pv=pvmList[i];
                var pvKey;
                $.each(pv, function(j) {
                    pvKey=j;
                });
                ids+=pvKey+':'+$('#'+pvKey+'').val()+',';
            }

            data.field.ids=ids;


            $.ajax({
                async: false,
                type: "post",
                dataType: "json",
                url: webUrl + '/task/audit', //url
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(data.field),
                success: function(result) {
                    if (result.data.success == true) {
                        if (result.data.endEvent == 'endEvent') {
                            updateFormKey(result.data.processInstanceId, result.data.state, result.data.url);
                        }
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                        window.parent.location.reload();
                    } else {
                        layer.msg("操作失败");
                    }
                },
                error: function() {
                    layer.msg("操作失败");
                }

            })

        })

        function updateFormKey(processInstanceId, state, url) {
            var data = {
                'processInstanceId': processInstanceId,
                'state': state
            }
            $.ajax({
                async: false,
                type: "post",
                dataType: "json",
                url: webUrl + url, //url
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(data),
                success: function(result) {
                    if (result.data.success == true) {

                    }
                },
                error: function() {
                    layer.msg("操作失败");
                }

            })
        }


        function selectPv(listName){
            if(listName.length>0){
                for(var i=0; i<listName.length; i++){
                    var pv=listName[i];
                    var pvKey;
                    var pvValue;
                    $.each(pv, function(i) {
                        pvKey=i;
                        pvValue=pv[i];
                    });
                    var html= '<div class="layui-form-item">'+
                        '<label class="layui-form-label">'+pvValue+'审核人：</label>'+
                        '<div class="layui-input-inline">'+
                        '<select name="'+pvKey+'" id="'+pvKey+'" >'+
                       ' </select></div></div>';

                    $("#sp").append(html);

                    var option='<option value=""></option>'
                    for(var j in userList){
                        var users=userList[j];
                        var userKey;
                        var userValue;
                        $.each(users,function(k){
                            userKey=users[0];
                            userValue=users[1];
                        });
                        option+='<option value="'+userKey+'">'+userValue+'</option>';
                    }
                    $('#'+pvKey+'').html(option);

                }
            }
        }


        //此处即为 radio 的监听事件
        form.on('radio(selectRadio)', function(data){

            var state = data.value;//被点击的radio的value值
            if(state=="2"){
                $('#spp').html('');
                $("#sp").show();
            }else{
                $('#spp').html('<input type="hidden" name="endEvent" value="endEvent">');
                $("#sp").hide();
            }

        });
       $("#userId").val(window.localStorage.getItem("userId"));
    });

    function getUrlParam(name) {
        var url = window.location.search; //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                //就是这句的问题
                theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
                //之前用了unescape()
                //才会出现乱码
            }
        }
        return theRequest[name];
    }


    //时间转换函数
    function showTime(tempDate)
    {
        var d = new Date(tempDate);
        var year = d.getFullYear();
        var month = d.getMonth();
        month++;
        var day = d.getDate();
        var hours = d.getHours();

        var minutes = d.getMinutes();
        var seconds = d.getSeconds();
        month = month<10 ? "0"+month:month;
        day = day<10 ? "0"+day:day;
        hours = hours<10 ? "0"+hours:hours;
        minutes = minutes<10 ? "0"+minutes:minutes;
        seconds = seconds<10 ? "0"+seconds:seconds;


        var time = year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
        return time;
    }

</script>
</body>
</html>